package com.kingyea.mobilepolice.apimanage.test.service;

import com.alibaba.fastjson.JSON;
import com.kingyea.mobilepolice.apimanage.test.bean.Test;
import com.kingyea.mobilepolice.apimanage.utils.ws.bean.AttachmentWrapper;

import javax.activation.DataHandler;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;

//@WebService//(endpointInterface = "com.kingyea.mobilepolice.apimanage.test.service.HelloWorld", serviceName = "cxfDemo")
public class HelloWorldImpl implements HelloWorld {


    @Override
    public String say() {
        System.out.println("say方法被调用。。。");
        return "hello," + ",welcome to this world,id=";
    }

    @Override
    public String speak() {
        System.out.println("speak方法被调用。。。");
        return "i want to speak " + " !";
    }

    @Override
    public String sayHello(String foo) {
        System.out.println("sayHello--");
        System.out.println("exec..." + foo);
        System.out.println(foo.length());
        System.out.println(foo.toString());
        return "hello " + foo.toString();
    }

    @Override
    public String saySelfInfo(String name, Integer age) {
        System.out.println("My name is " + name + " ,  I am " + age + " years old");
        return "My name is " + name + " ,  I am " + age + " years old";
    }

    @Override
    public Test saySomeThings(String name, Integer age, Double money, String aaa) {
        System.out.println(JSON.toJSONString(new Object[]{name, age, money, aaa}));
        return new Test(1, "tom", 12);
    }

    @Override
    public String talkNonsense(HashMap<String, Object> map) {
        System.out.println(JSON.toJSONString(map));
        return JSON.toJSONString(map);
    }


    @Override
    public String upload(String xml, final List<AttachmentWrapper> attachments) {
        try {
            System.out.println(xml + attachments.toString());
            InputStream ins = attachments.get(0).getFile().getInputStream();


            FileOutputStream out = new FileOutputStream("d:\\A\\a.zip");
            byte[] b = new byte[1024];
            int n = 0;
            while ((n = ins.read(b)) != -1) {
                out.write(b, 0, n);
            }
            ins.close();
            out.close();


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return JSON.toJSONString(xml + attachments.toString());
        }
    }

    public boolean uploadFile(String fileName, DataHandler dataHandler) {
        OutputStream os = null;
        try {
            os = new FileOutputStream("d:\\A\\" + fileName);
            dataHandler.writeTo(os);//大附件也会出现内存溢出
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    public Test test1() {

        return new Test(1, "tom", 99);
    }


}
